package com.qbw.core.base;


import android.app.Activity;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.http.okhttp.HttpTask;
import com.qbw.core.view.ProgressView;
import com.qbw.core.view.RefreshLoadLayout;

import java.lang.ref.WeakReference;

/**
 * Created by Bond on 2017/02/15 10:47
 * you can contact me at qbaowei@qq.com
 */

public abstract class BasePresenter<VIEW> implements HttpTask.CallBack {

    private WeakReference<VIEW> mWrView;

    public BasePresenter(VIEW VIEW) {
        mWrView = new WeakReference<>(VIEW);
    }

    public void onCreate() {

    }

    @Override
    public void onHttpStart(HttpTask httpTask) {

    }

    @Override
    public void onHttpFailed(HttpTask httpTask, int errorCode, String message) {
        VIEW view = getView();
        if(view == null) {
            return;
        }
        onFailed(httpTask, errorCode, message);
    }

    @Override
    public void onHttpSuccess(HttpTask httpTask, Object entity) {
        VIEW view = getView();
        if(view == null) {
            return;
        }
        onSuccess(httpTask, entity);
    }

    protected void onSuccess(HttpTask httpTask, Object entity){}

    protected void onFailed(HttpTask httpTask, int errorCode, String message){}

    @Nullable
    public VIEW getView() {
        VIEW VIEW = mWrView.get();
        if (VIEW == null) {
            return null;
        }
        if (VIEW instanceof Activity && ((Activity) VIEW).isFinishing()) {
            return null;
        } else if (VIEW instanceof Fragment) {
            Activity activity = ((Fragment) VIEW).getActivity();
            if (activity == null || activity.isFinishing()) {
                return null;
            }
        }
        return VIEW;
    }

    protected void finishActivity() {
        VIEW VIEW = mWrView.get();
        if (VIEW == null) {
            return;
        }
        if (VIEW instanceof Activity) {
            ((Activity) VIEW).finish();
        } else if (VIEW instanceof Fragment) {
            Activity activity = ((Fragment) VIEW).getActivity();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    protected RefreshLoadLayout getRefreshLayout() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof IRefreshLoadView) {
            return ((IRefreshLoadView) VIEW).getRefreshLoadLayout();
        }
        return null;
    }

    protected RecyclerView getRecyclerView() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof IRefreshLoadView) {
            return ((IRefreshLoadView) VIEW).getRecyclerView();
        }
        return null;
    }

    protected BaseAdapter getAdapter() {
        RecyclerView rcv = getRecyclerView();
        if (rcv != null && rcv.getAdapter() != null) {
            return (BaseAdapter) rcv.getAdapter();
        }
        return null;
    }

    protected Context getContext() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof IContext) {
            return ((IContext) VIEW).getContext();
        }
        return null;
    }

    protected ProgressView getProgressView() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof IProgressView) {
            return ((IProgressView) VIEW).getProgressView();
        }
        return null;
    }

    /*protected void showProgress(boolean b) {
        ProgressView pv = getProgressView();
        if (pv != null) {
            pv.setVisibility(b ? View.VISIBLE : View.GONE);
        }
    }*/

    /*protected SlidingTabLayout getSlidingTabLayout() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof ITabView) {
            return ((ITabView) VIEW).getSlidingTabLayout();
        }
        return null;
    }*/

    protected ViewPager getViewPager() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof ITabView) {
            return ((ITabView) VIEW).getViewPager();
        }
        return null;
    }

    protected FragmentManager getFragmentManager() {
        VIEW VIEW = getView();
        if (VIEW != null && VIEW instanceof BaseActivity) {
            return ((BaseActivity) VIEW).getSupportFragmentManager();
        }
        return null;
    }

    public void onDestroy() {

    }

    public interface IContext {
        Context getContext();
    }

    public interface IProgressView {
        ProgressView getProgressView();
    }

    public interface IRefreshLoadView {
        RefreshLoadLayout getRefreshLoadLayout();

        RecyclerView getRecyclerView();
    }

    public interface ITabView {

        //SlidingTabLayout getSlidingTabLayout();

        ViewPager getViewPager();

        //FragmentManager getFragmentManager();
    }

    protected void showProgress(boolean b) {
        Object v = getView();
        if (v != null && v instanceof IProgress) {
            ((IProgress) v).showProgress(b);
        }
    }

    public interface IProgress {
        void showProgress(boolean b);
    }
}
